/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-13
 * Time: 18:38
 */
public class Sort {

    public static void insertSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int j = i - 1;
            int tmp = arr[i];
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                }else {
                    break;
                }
            }
            arr[j + 1] = tmp;
        }
    }

    public static void shellSort(int[] arr) {
        //进行分组
        int gap = arr.length/2;
        while (gap >= 1) {
            shellInsertSort(arr,gap);
            gap /= 2;
        }
    }
    //每次分组后进行的直接插入排序
    public static void shellInsertSort(int[] arr,int gap) {
        for (int i = gap; i < arr.length; i++) {
            int j = i - gap;
            int tmp = arr[i];
            for (; j >= 0; j -= gap) {
                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                }else {
                    break;
                }
            }
            arr[j + gap] = tmp;
        }
    }
}
